package com.zy.explore.generator.infrastructure.core.brick;

import com.zy.explore.generator.infrastructure.core.assembler.BrickInfoAssembler;
import com.zy.explore.generator.infrastructure.core.base.BaseModule;
import com.zy.explore.generator.infrastructure.core.enums.FrameworkTypeEnum;
import com.zy.explore.generator.infrastructure.core.frm.BrickInfoFrm;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;

/**
 * description 生成Router
 *
 * @author 🇨🇳中国印🇨🇳
 * @date 2021年05月11日 21:53
 **/
@Slf4j
@Service
public class GenerationRouter extends BaseModule {

    public void doGeneration(BrickInfoFrm frm) throws Exception {
        if (frm.getUseJpa()) {
            genJpaRouter(frm);
        }
        if (frm.getUseTkMybatis()) {
            genTkRouter(frm);
        }
        if (frm.getUseMybatisPlus()) {
            genMpRouter(frm);
        }
    }

    private void genMpRouter(BrickInfoFrm frm) throws IOException, TemplateException {
        frm.setFileName((frm.getTrimPrefix() ? "" : "Mp") + frm.getTableClassNamePrefix() + "Router.java");
        File file;
        // 要生成的文件对象(文件绝对位置 + 文件名称)
        if (frm.getFrameworkType() == FrameworkTypeEnum.DDD_FEIGN) {
            file = new File(frm.getFeignCodeRoot(), BrickInfoAssembler.getLastFilePath(frm));
        } else {
            file = new File(frm.getProviderCodeRoot(), BrickInfoAssembler.getLastFilePath(frm));
        }

        // 获取模板文件的位置
        String templatePath = BrickInfoAssembler.getBrickTemplatePath(frm) + "/mybatisplus/";

        // 写入文件
        writeFile(file, "MpRouter.ftl", templatePath, frm);

        log.info("创建路由文件Mp{}Router.java {}", frm.getTableClassNamePrefix(), file.getPath());
    }

    private void genTkRouter(BrickInfoFrm frm) throws IOException, TemplateException {
        frm.setFileName((frm.getTrimPrefix() ? "" : "Tk") + frm.getTableClassNamePrefix() + "Router.java");
        File file;
        // 要生成的文件对象(文件绝对位置 + 文件名称)
        if (frm.getFrameworkType() == FrameworkTypeEnum.DDD_FEIGN) {
            file = new File(frm.getFeignCodeRoot(), BrickInfoAssembler.getLastFilePath(frm));
        } else {
            file = new File(frm.getProviderCodeRoot(), BrickInfoAssembler.getLastFilePath(frm));
        }

        // 获取模板文件的位置
        String templatePath = BrickInfoAssembler.getBrickTemplatePath(frm) + "/tkmybatis/";

        // 写入文件
        writeFile(file, "TkRouter.ftl", templatePath, frm);

        log.info("创建路由文件Tk{}Router.java {}", frm.getTableClassNamePrefix(), file.getPath());
    }

    private void genJpaRouter(BrickInfoFrm frm) throws IOException, TemplateException {
        frm.setFileName((frm.getTrimPrefix() ? "" : "Jpa") + frm.getTableClassNamePrefix() + "Router.java");
        File file;
        // 要生成的文件对象(文件绝对位置 + 文件名称)
        if (frm.getFrameworkType() == FrameworkTypeEnum.DDD_FEIGN) {
            file = new File(frm.getFeignCodeRoot(), BrickInfoAssembler.getLastFilePath(frm));
        } else {
            file = new File(frm.getProviderCodeRoot(), BrickInfoAssembler.getLastFilePath(frm));
        }

        // 获取模板文件的位置
        String templatePath = BrickInfoAssembler.getBrickTemplatePath(frm) + "/jpa/";

        // 写入文件
        writeFile(file, "JpaRouter.ftl", templatePath, frm);

        log.info("创建路由文件Jpa{}Router.java {}", frm.getTableClassNamePrefix(), file.getPath());
    }

}
